package 数组和链表.linklist;

import 抽象数据类型.Node;

/**
 * 链表，相当于火车
 *
 * @author hunbird
 */
public class LinkList {
	//头节点
	private Node first;

	public LinkList() {
		first = null;
	}

	//插入一个节点，在头节点进行插入操作
	public void insertFirst(int value) {
		Node node = new Node(value);
		node.next = first;
		first = node;
	}

	//在单链表末尾添加元素
	public void inset(int value) {
		Node node = new Node(value);
		Node tmp = first;
		while (tmp.next != null) {
			tmp = tmp.next;

		}

		tmp.next = node;
	}

	//显示方法
	public void display() {
		Node current = first;
		while (current != null) {
			current.display();
			current = current.next;
		}
		System.out.println();
	}

	//删除一个节点，在头节点后进行删除
	public Node deleteFirst() {
		Node tmp = first;
		first = tmp.next;
		return tmp;
	}

	//查找方法
	public Node find(Comparable value) {
		Node current = first;
		while (current.data != value) {
			if (current.next == null) {
				return null;
			}
			current = current.next;
		}
		return current;
	}

	//根据输入的数据进行的删除操作
	public Node delete(Comparable value) {
		Node current = first;
		Node previous = first;
		while (current.data != value) {
			if (current.next == null) {
				return null;
			}
			previous = current;
			current = current.next;
		}
		if (current == first) {
			first = first.next;
		} else
		{
			previous.next=current.next;
		}
		return current;
	}

}
